package com.fla.shapeevader.ui.drawers;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Style;

import com.fla.shapeevader.model.BaseShape;

public class CircleDrawer extends BaseShapeDrawer {

    public CircleDrawer(BaseShape shape, int color) {
        super(shape, color);

        if (shape.getBounds().width() != shape.getBounds().height()) {
            throw new IllegalArgumentException("CircleDrawer supports only square shape, not any rectangle");
        }
    }

    @Override
    public boolean checkIntersection(BaseShapeDrawer otherShape) {
        RectF other = otherShape.getShape().getBounds();
        
        boolean result = 
            isInsideCircle(other.left, other.top) 
            || isInsideCircle(other.left, other.bottom)
            || isInsideCircle(other.right, other.top) 
            || isInsideCircle(other.right, other.bottom);

        return result ? otherShape.checkIntersection(this) : false;
    }

    private boolean isInsideCircle(float x, float y) {
        return Math.pow(shape.getBounds().centerX() - x, 2) + Math.pow(shape.getBounds().centerY() - y, 2) <= Math.pow(
                shape.getBounds().width() / 2, 2);
    }

    @Override
    public void drawOnCanvas(Canvas canvas) {
        Paint paint = new Paint();
        paint.setStrokeWidth(1);
        paint.setStyle(Style.FILL);
        paint.setColor(color);

        canvas.drawOval(shape.getBounds(), paint);
    }

}
